package com.skynet.supervision.utils;

import lombok.AllArgsConstructor;
import lombok.Data;

import javax.annotation.Nonnull;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

@Data
@AllArgsConstructor
public class Box<T> {

    private T data;

    public T get() {
        return data;
    }

    public boolean isEmpty() {
        return data == null;
    }

    public boolean isEmpty(@Nonnull Function<T, Boolean> custom) {
        return custom.apply(data);
    }

    public void set(T data) {
        this.data = data;
    }

    public static <T> Box<T> empty() {
        return new Box<>(null);
    }

    public static <T> Box<T> of(T obj) {
        return new Box<>(obj);
    }

    public Optional<T> optional() {
        return Optional.ofNullable(data);
    }

    public Stream<T> stream() {
        return optional().stream();
    }
}
